package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Transactional
@Service(interfaceClass = CheckGroupService.class)
public class CheckGroupServiceImp implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;

    //添加检查组和与检查项的关联关系
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        checkGroupDao.add(checkGroup);
        Integer checkGroupId = checkGroup.getId();
        for (Integer checkitemId : checkitemIds) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("checkGroupId", checkGroupId);
            map.put("checkitemId", checkitemId);
            checkGroupDao.setCheckGroupAndCheckItem(map);
        }
    }

    //    分页查询
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //使用分页助手进行分页
        PageHelper.startPage(currentPage, pageSize);
        //根据条件查询
        if (queryString == null || queryString == "null") {
            queryString = "";
        }
        Page<CheckGroup> page = checkGroupDao.findByCondition("%" + queryString + "%");
        long total = page.getTotal();
        List<CheckGroup> list = page.getResult();
        return new PageResult(total, list);
    }

    //根据检查组id查询该检查组
    @Override
    public CheckGroup findById(Integer id) {
        CheckGroup checkGroup=checkGroupDao.findById(id);
        return checkGroup;
    }
    //根据检查组id查询包含的检查项
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        List<Integer> list=checkGroupDao.findCheckItemIdsByCheckGroupId(checkGroupId);
        return list;
    }

    //编辑检查组和与检查项的关联关系
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        Integer checkGroupId = checkGroup.getId();
        //编辑检查组
        checkGroupDao.edit(checkGroup);
        //删除所有检查项的关联关系
        checkGroupDao.deleteAssociationByCheckGroupId(checkGroupId);
        //新增与检查项的关联关系
        for (Integer checkitemId : checkitemIds) {
            HashMap<String,Integer> map = new HashMap<>();
            map.put("checkGroupId",checkGroupId);
            map.put("checkitemId",checkitemId);
            checkGroupDao.addAssociationWithCheckItem(map);
        }

    }

    @Override
    public List<CheckGroup> findAll() {
        List<CheckGroup> list=checkGroupDao.findAll();
        return list;
    }

    @Override
    public void delete(Integer id) {
        //根据检查组id从关系表中删除所有与检查项的关联
        checkGroupDao.deleteAssociationByCheckGroupId(id);
        //根据id从检查组表中删除指定检查组
        checkGroupDao.deleteById(id);
    }
}
